#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
export DH_VERBOSE = 1


# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

name := cart
prefix := /usr
exec_prefix := $(prefix)
buildroot := debian/tmp
carthome := $(exec_prefix)/lib/$(DEB_BUILD_MULTIARCH)/$(name)

SCONS = scons
DEB_SCONS_OPTIONS := --config=force USE_INSTALLED=all \
	PREFIX=$(buildroot)$(prefix)

%:
	dh $@

override_dh_auto_build:
	if [ ! -e src/$(name)/_structures_from_macros_.h_orig ] ;then \
	  cp src/$(name)/_structures_from_macros_.h \
	    src/$(name)/_structures_from_macros_.h_orig ;fi
	if [ ! -e src/SConscript_orig ] ; then \
	  find . -name SConscript -exec cp -v '{}' '{}_orig' ';' ;fi
	find . -name SConscript | \
	  xargs sed -i -e '/AppendUnique(RPATH=.*)/d'
	$(SCONS) $(DEB_SCONS_OPTIONS)

override_dh_auto_clean:
	$(SCONS) --clean
	rm -rf build install
	find . -name '*.pyc' -delete
	rm -rf _build.external-Linux
	rm -rf .sconf-temp-Linux
	rm -f .sconsign-Linux.dblite
	rm -f .build_vars-Linux.json .build_vars-Linux.sh
	rm -f $(name)-Linux.conf
	rm -f config-Linux.log
	if [ -e src/$(name)/_structures_from_macros_.h_orig ] ;then \
	  mv src/$(name)/_structures_from_macros_.h_orig \
	    src/$(name)/_structures_from_macros_.h ;fi
	if [ -e src/SConscript_orig ] ; then \
	  find . -name SConscript -exec mv -v -f '{}_orig' '{}' ';' ;fi
	dh_auto_clean

# cp openmpi/include to work around bug until ompi dependency removed
# Need to save/restore src/cart/_structures_from_macros_.h
override_dh_auto_install:
	if [ ! -e src/$(name)/_structures_from_macros_.h_orig ] ;then \
	  cp src/$(name)/_structures_from_macros_.h \
	    src/$(name)/_structures_from_macros_.h_orig ;fi
	if [ ! -e src/SConscript_orig ] ; then \
	  find . -name SConscript -exec cp -v '{}' '{}_orig' ';' ;fi
	find . -name SConscript | \
	  xargs sed -i -e '/AppendUnique(RPATH=.*)/d'
	mkdir -p $(buildroot)$(prefix)/include
	$(SCONS) $(DEB_SCONS_OPTIONS) install
	find . -name src/SConscript -exec mv -v -f '{}_orig' '{}' ';'
	sed -i -r -e s%.+$(buildroot)$(prefix)%$(prefix)%g \
	  $(buildroot)$(prefix)/TESTING/.build_vars.*
	for f in cart_ctl self_test ; do \
	  mv $(buildroot)$(prefix)/bin/$$f \
	    $(buildroot)$(prefix)/bin/$(DEB_BUILD_MULTIARCH)-$$f ; \
	  ln -s $(DEB_BUILD_MULTIARCH)-$$f $(buildroot)$(prefix)/bin/$$f ;done
	mkdir -p $(buildroot)$(carthome)
	mkdir -p $(buildroot)$(prefix)/lib/$(DEB_BUILD_MULTIARCH)
	cp -al multi-node-test.sh utils $(buildroot)$(carthome)/
	mv $(buildroot)$(prefix)/TESTING $(buildroot)$(carthome)/
	ln $(buildroot)$(carthome)/TESTING/.build_vars.sh \
	  $(buildroot)$(carthome)/.build_vars-linux.sh
	mv $(buildroot)$(prefix)/lib/lib*.so* \
	  $(buildroot)$(prefix)/lib/$(DEB_BUILD_MULTIARCH)/
	rm -rf $(buildroot)$(carthome)/utils/docker
	chmod 0644 $(buildroot)$(carthome)/TESTING/*/*.yaml
	rm -rf build install
	find . -name '*.pyc' -delete

